//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		$('#inboundPlanDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#inboundPlanDatagrid').datagrid({ 
			queryParams:{
				order_no:order_no
			}
		});
	})
	//账册商品搜索
	$('.goodssearchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var source_no=thisform.find('input[name=source_no]').val();
		$('#indexgoodsDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#indexgoodsDatagrid').datagrid({ 
			queryParams:{
				goods_no:goods_no,
				goods_name:goods_name,
				source_no:source_no
			}
		});
	})
	//更新行数据
	function inboundPlanUpdateActions(value){
		$.post(inboundPlanEditUrl,{id:value.id,name:value.name,cate:value.cate,erp_code:value.erp_code},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#inboundPlanDatagrid').datagrid('validateRow', editIndex)){
			$('#inboundPlanDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#inboundPlanDatagrid').datagrid('selectRow', index);
				$('#inboundPlanDatagrid').datagrid('endEdit', editIndex);
				$('#inboundPlanDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#inboundPlanDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#inboundPlanDatagrid').datagrid({
		url:getinboundPlanUrl,
		title:'入库单',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'单号',sortable:true},
		{field:'batch_no',title:'批次号',sortable:true},
		{field:'old_order_no',title:'原单号',sortable:true},
		{field:'manual_id',title:'账册编号'},
		{field:'receive_no',title:'核放单号'},
		{field:'customs_code',title:'关区代码'},
		{field:'gross_weight',title:'毛重'},
		{field:'net_weight',title:'净重'},
		{field:'amount',title:'件数'},
		// {field:'wrap_type',title:'包装种类'},
		{field:'port',title:'申报关区'},
		// {field:'expected_time',title:'预计到货时间',sortable:true},
		{field:'add_time',title:'接收时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true},
		{field:'status',title:'审核状态',sortable:true},
		{field:'inbound_status',title:'入库',sortable:true,
				formatter:function(value,row,index){
					if(value==0){
						return '否';
					}
					if(value==1){
						return '是';
					}
				}
		},
		{field:'plan_print_status',title:'计划打印状态',sortable:true,
				formatter:function(value,row,index){
					if(value==0){
						return '未打印';
					}
					if(value==1){
						return '已打印';
					}
				}
		},
		{field:'print_status',title:'实收打印状态',sortable:true,
				formatter:function(value,row,index){
					if(value==0){
						return '未打印';
					}
					if(value==1){
						return '已打印';
					}
				}
		}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					inboundPlanUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
	
	$('#arrivalOrderDatagrid').datagrid({
		url:getArrivalOrderUrl,
		fitColumns:true,
		rownumbers:true,
		columns:[[
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'goods_name',title:'商品名称'},
		{field:'declare_amount',title:'数量',sortable:true},
		{field:'price',title:'价格',sortable:true},
		{field:'country_code',title:'原产国',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getCountryUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.country_name;
				},
		}
		]],
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		}
	});
});
inboundPlanObj={
	save:function(url){//编辑后保存数据
		$('#inboundPlanEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#inboundPlanEditDialog').dialog('close');
                    });
                }else{
                    $('#inboundPlanEditDialog').dialog('close');      
                    $('#inboundPlanDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#inboundPlanDatagrid').datagrid('validateRow', editIndex)){
			$('#inboundPlanDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#inboundPlanDatagrid').datagrid('acceptChanges');
		}
	},
	showgoodsAll:function(){//显示默认条件下所有数据
		$('#indexgoodsDatagrid').datagrid({ queryParams:''});
	},
	showAll:function(){//显示默认条件下所有数据
		$('#inboundPlanDatagrid').datagrid({ queryParams:''});
	},
	infoBar:function(){//显示详情
		var inboundPlanRow=$('#inboundPlanDatagrid').datagrid('getSelections');
        if (inboundPlanRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(inboundPlanRow.length==1){
			var id=inboundPlanRow[0]['id'];
        	$('#inboundPlanlistDialog').dialog('open').dialog('setTitle','详情');
			$('#arrivalOrderDatagrid').datagrid({ 
				queryParams:{
					id:id
				}
			});
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	planBar:function(value){
		var inboundPlanRow=$('#inboundPlanDatagrid').datagrid('getSelections');
        if (inboundPlanRow.length>1){
			$.messager.alert('提示','一次只能选择一条记录','error');
		}else if(inboundPlanRow.length==1){
            	var id=inboundPlanRow[0]['id'];
				window.parent.showTab(planUrl,'监盘计划',id,value);
		}else{
			$.messager.alert('提示','请选择列表数据','error');
		}
	},
	tallyBar:function(value){
		var inboundPlanRow=$('#inboundPlanDatagrid').datagrid('getSelections');
        if (inboundPlanRow.length>1){
			$.messager.alert('提示','一次只能选择一条记录','error');
		}else if(inboundPlanRow.length==1){
            	var id=inboundPlanRow[0]['id'];
				if(value==1){
					window.parent.showTab(tallyUrl,'理货报告',id,value);
				}
				if(value==2){
					window.parent.showTab(tallyUrl,'理货报告',id,value);
				}
				if(value==3 ||　value==4){
					// window.parent.showTab(tallyUrl,'理货报告',id,value);
					window.open(tallyUrl+"&id="+id+"&cid="+value);
				}
		}else{
			$.messager.alert('提示','请选择列表数据','error');
		}
	},
	reloadBar:function(){//刷新数据
		$('#inboundPlanDatagrid').datagrid('reload');
	},
	examineBar:function(value){//审核数据
		var inboundPlanRow=$('#inboundPlanDatagrid').datagrid('getSelections');
		if(value==1){
			var text='确定审核通过吗！';
		}
		if(value==2){
			var text='确定审核不通过吗！';
		}
        if (inboundPlanRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<inboundPlanRow.length;i++){
                		ids.push(inboundPlanRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(inboundPlanExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#inboundPlanDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	pushBar:function(){
		var Row=$('#inboundPlanDatagrid').datagrid('getSelections');
		var text='确定申报吗！';
		if(Row[0].status!='审核通过'){
			$.messager.alert('提示','未审核通过不能申报','error');
			return false;
		}
		if(Row[0].plan_apply_status==1){
			$.messager.alert('提示','已申报通过不能重复申报','error');
			return false;
		}
        if (Row.length==1){
        	$.messager.confirm('提示',text,function(r){
                if (r){
					$.post(pushOrderUrl,{plan_receipts_id:Row[0].order_no,id:Row[0].id,receive_no:Row[0].receive_no},function(result){
						if (result.flag){
							$.messager.alert('推送成功',result.tip);
						}else{
							$.messager.alert('错误提示',result.tip,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.tip,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else if(Row.length>1){
			$.messager.alert('提示','一次只能选择一条记录','error');
		}else{
			$.messager.alert('提示','请选择列表数据','error');
		}
	},
	pushreceiveamountBar:function(){
		var Row=$('#inboundPlanDatagrid').datagrid('getSelections');
		var text='确定申报吗！';
		if(Row[0].status!='审核通过'){ 
			$.messager.alert('提示','未审核通过不能申报','error');
			return false;
		}
		if(Row[0].receive_apply_status==1){
			$.messager.alert('提示','已申报通过不能重复申报','error');
			return false;
		}
        if (Row.length==1){
        	$.messager.confirm('提示',text,function(r){
                if (r){
					$.post(pushOrderreceiveamountUrl,{plan_receipts_id:Row[0].order_no,id:Row[0].id,receive_no:Row[0].receive_no},function(result){
						if (result.flag){
							$.messager.alert('推送成功',result.tip);
						}else{
							$.messager.alert('错误提示',result.tip,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.tip,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else if(Row.length>1){
			$.messager.alert('提示','一次只能选择一条记录','error');
		}else{
			$.messager.alert('提示','请选择列表数据','error');
		}
	},
	inboundBar:function(){
		var confirmArrivalRow=$('#inboundPlanDatagrid').datagrid('getSelections');
		var text='确定入库吗！';
        if (confirmArrivalRow.length>0){
        	$.messager.confirm('入库提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<confirmArrivalRow.length;i++){
                		ids.push(confirmArrivalRow[i].id);
						if(confirmArrivalRow[i].inbound_status==1){
							$.messager.alert('错误提示','单号"'+confirmArrivalRow[i].order_no+'"已经入库，不能选择','error');
							return false;
						}
                	}
                	var id=ids.join(',');
                    $.post(arrivalinboundUrl,{id:id},function(result){
                        if (result.status){
							$.messager.confirm('错误提示',result.info,function(r){
								$('#inboundPlanDatagrid').datagrid('reload');
							});
                            
                        }else{
                            $.messager.alert('错误提示',result.info,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',result.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	excelBar:function(){//导出excel
		var Row=$('#inboundPlanDatagrid').datagrid('getSelections');
		var text='确定导出吗！';
        if (Row.length==1){
        	$.messager.confirm('提示',text,function(r){
                if (r){
					url=getinboundPlanDetailUrl+"&excel=1&rows=6000&id="+Row[0].id;
					window.location.href=url;
                }
        	});
		}else if(Row.length>1){
			$.messager.alert('提示','一次只能选择一条记录','error');
		}else{
			$.messager.alert('提示','请选择列表数据','error');
		}
		
	},
	addsbar:function(){
		var goodsRow=$('#indexgoodsDatagrid').datagrid('getSelections');
		// console.log(order_id);
		
        if (goodsRow.length>1){
            $.messager.alert('提示','一次只能选择一条记录!','info');
        }else if(goodsRow.length==1){
			$.ajax({
				type:"POST",
				url:adddgoods_url,
				data:{goods_id:goodsRow[0]['id'],order_id:order_id},
				success: function(result){
					if(result.status==1){
						$.messager.alert('推送提示',result.message,'info');
					}else{
						$.messager.alert('推送提示',result.message,'error');
					}
				}
			});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	moreBar:function(){//多到货
		var Row=$('#inboundPlanDatagrid').datagrid('getSelections');
        if(Row.length>1){
            $.messager.alert('提示','一次只能选择一条记录!','info');
        }else if(Row.length==1){
			order_id=Row[0]['id'];
			// console.log(order_id);
        	$('#indexgoodsAddDialog').dialog('open').dialog('setTitle','商品列表');
			$('#indexgoodsDatagrid').datagrid({
				url:getbooksgoodsUrl,
				fitColumns:true,
				rownumbers:true,
				pageSize:25,
				pageNumber:1,
				pageList:[1,2,5,10,15,20,25,30,40,50],
				columns:[[
				{field:'id',title:'id',checkbox:true},
				{field:'goods_no',title:'商品编码',sortable:true},
				{field:'manua_id',title:'账册编码',sortable:true},
				{field:'goods_name',title:'商品名称',sortable:true},
				{field:'source_no',title:'料号',sortable:true},
				{field:'goods_spec',title:'规格',sortable:true}
				]],
				onLoadError:function(data){
					var info=eval('('+data.responseText+')');
					$.messager.confirm('错误提示',info.message,function(r){
						var tab=$('#Tabs').tabs('getSelected');
						var index=$('#Tabs').tabs('getTabIndex',tab);
						$('#Tabs').tabs('close',index);
					});
				},
				pagination:true,
				fit:true,
			});
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}

